home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / source / chapter24 / isohex24_1 / isohex24_1.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-16  |  8.9 KB  |  347 lines

  1. /*****************************************************************************
  2. IsoHex24_1.cpp
  3. Ernest S. Pazera
  4. 14NOV2000
  5. Start a WIN32 Application Workspace, add in this file
  6. Needs ddraw.lib, d3d8.lib and dxguid.lib
  7. Needs GDICanvas.h/cpp
  8. Needs DDFuncs.h/cpp
  9. *****************************************************************************/
  10.  
  11. //////////////////////////////////////////////////////////////////////////////
  12. //INCLUDES
  13. //////////////////////////////////////////////////////////////////////////////
  14. #define WIN32_LEAN_AND_MEAN  
  15.  
  16. #include <windows.h>   
  17. #include <math.h>//sin and cos
  18. #include "GDICanvas.h"
  19. #include "ddraw.h"
  20. #include "DDFuncs.h"
  21. #include "d3d.h"
  22.  
  23. //////////////////////////////////////////////////////////////////////////////
  24. //DEFINES
  25. //////////////////////////////////////////////////////////////////////////////
  26. //name for our window class
  27. #define WINDOWCLASS "ISOHEX24"
  28. //title of the application
  29. #define WINDOWTITLE "IsoHex 24-1"
  30.  
  31. //screen attributes
  32. const DWORD SCREENWIDTH=640;
  33. const DWORD SCREENHEIGHT=480;
  34. const DWORD SCREENBPP=16;
  35.  
  36. //pi
  37. const double PI=3.14159;
  38.  
  39. //////////////////////////////////////////////////////////////////////////////
  40. //PROTOTYPES
  41. //////////////////////////////////////////////////////////////////////////////
  42. bool Prog_Init();//game data initalizer
  43. void Prog_Loop();//main game loop
  44. void Prog_Done();//game clean up
  45.  
  46. //////////////////////////////////////////////////////////////////////////////
  47. //GLOBALS
  48. //////////////////////////////////////////////////////////////////////////////
  49. HINSTANCE hInstMain=NULL;//main application handle
  50. HWND hWndMain=NULL;//handle to our main window
  51.  
  52. //IDirectDraw7 Pointer
  53. LPDIRECTDRAW7 lpdd=NULL;
  54.  
  55. //surfaces
  56. LPDIRECTDRAWSURFACE7 lpddsPrime=NULL;
  57. LPDIRECTDRAWSURFACE7 lpddsBack=NULL;
  58.  
  59. //IDirect3D7
  60. LPDIRECT3D7 lpd3d=NULL;
  61.  
  62. //IDirect3DDevice
  63. LPDIRECT3DDEVICE7 lpd3ddev=NULL;
  64.  
  65. //vertices
  66. D3DTLVERTEX vert[3];//three vertices
  67. //angle, used for vertex calculations
  68. double angle=0.0;
  69.  
  70. //////////////////////////////////////////////////////////////////////////////
  71. //WINDOWPROC
  72. //////////////////////////////////////////////////////////////////////////////
  73. LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  74. {
  75.     //which message did we get?
  76.     switch(uMsg)
  77.     {
  78.     case WM_KEYDOWN:
  79.         {
  80.             //check for escape key
  81.             if(wParam==VK_ESCAPE)
  82.             {
  83.                 DestroyWindow(hWndMain);
  84.             }
  85.  
  86.             return(0);//handled message
  87.         }break;
  88.     case WM_DESTROY://the window is being destroyed
  89.         {
  90.  
  91.             //tell the application we are quitting
  92.             PostQuitMessage(0);
  93.  
  94.             //handled message, so return 0
  95.             return(0);
  96.  
  97.         }break;
  98.     case WM_PAINT://the window needs repainting
  99.         {
  100.             //a variable needed for painting information
  101.             PAINTSTRUCT ps;
  102.             
  103.             //start painting
  104.             HDC hdc=BeginPaint(hwnd,&ps);
  105.  
  106.             /////////////////////////////
  107.             //painting code would go here
  108.             /////////////////////////////
  109.  
  110.             //end painting
  111.             EndPaint(hwnd,&ps);
  112.                         
  113.             //handled message, so return 0
  114.             return(0);
  115.         }break;
  116.     }
  117.  
  118.     //pass along any other message to default message handler
  119.     return(DefWindowProc(hwnd,uMsg,wParam,lParam));
  120. }
  121.  
  122.  
  123. //////////////////////////////////////////////////////////////////////////////
  124. //WINMAIN
  125. //////////////////////////////////////////////////////////////////////////////
  126. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
  127. {
  128.     //assign instance to global variable
  129.     hInstMain=hInstance;
  130.  
  131.     //create window class
  132.     WNDCLASSEX wcx;
  133.  
  134.     //set the size of the structure
  135.     wcx.cbSize=sizeof(WNDCLASSEX);
  136.  
  137.     //class style
  138.     wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  139.  
  140.     //window procedure
  141.     wcx.lpfnWndProc=TheWindowProc;
  142.  
  143.     //class extra
  144.     wcx.cbClsExtra=0;
  145.  
  146.     //window extra
  147.     wcx.cbWndExtra=0;
  148.  
  149.     //application handle
  150.     wcx.hInstance=hInstMain;
  151.  
  152.     //icon
  153.     wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
  154.  
  155.     //cursor
  156.     wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
  157.  
  158.     //background color
  159.     wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
  160.  
  161.     //menu
  162.     wcx.lpszMenuName=NULL;
  163.  
  164.     //class name
  165.     wcx.lpszClassName=WINDOWCLASS;
  166.  
  167.     //small icon
  168.     wcx.hIconSm=NULL;
  169.  
  170.     //register the window class, return 0 if not successful
  171.     if(!RegisterClassEx(&wcx)) return(0);
  172.  
  173.     //create main window
  174.     hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_POPUP | WS_VISIBLE,0,0,320,240,NULL,NULL,hInstMain,NULL);
  175.  
  176.     //error check
  177.     if(!hWndMain) return(0);
  178.  
  179.     //if program initialization failed, then return with 0
  180.     if(!Prog_Init()) return(0);
  181.  
  182.     //message structure
  183.     MSG msg;
  184.  
  185.     //message pump
  186.     for(;;)    
  187.     {
  188.         //look for a message
  189.         if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  190.         {
  191.             //there is a message
  192.  
  193.             //check that we arent quitting
  194.             if(msg.message==WM_QUIT) break;
  195.             
  196.             //translate message
  197.             TranslateMessage(&msg);
  198.  
  199.             //dispatch message
  200.             DispatchMessage(&msg);
  201.         }
  202.  
  203.         //run main game loop
  204.         Prog_Loop();
  205.     }
  206.     
  207.     //clean up program data
  208.     Prog_Done();
  209.  
  210.     //return the wparam from the WM_QUIT message
  211.     return(msg.wParam);
  212. }
  213.  
  214. //////////////////////////////////////////////////////////////////////////////
  215. //INITIALIZATION
  216. //////////////////////////////////////////////////////////////////////////////
  217. bool Prog_Init()
  218. {
  219.     lpdd=LPDD_Create(hWndMain,DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_ALLOWREBOOT);
  220.  
  221.     //set the display mode
  222.     lpdd->SetDisplayMode(SCREENWIDTH,SCREENHEIGHT,SCREENBPP,0,0);
  223.  
  224.     //create primary surface
  225.     DDSURFACEDESC2 ddsd;
  226.     memset(&ddsd,0,sizeof(DDSURFACEDESC2));
  227.     ddsd.dwSize=sizeof(DDSURFACEDESC2);
  228.     ddsd.dwFlags=DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
  229.     ddsd.dwBackBufferCount=1;
  230.     ddsd.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX | DDSCAPS_3DDEVICE;
  231.     lpdd->CreateSurface(&ddsd,&lpddsPrime,NULL);
  232.  
  233.     //create back buffer
  234.     DDSCAPS2 ddscaps;
  235.     memset(&ddscaps,0,sizeof(DDSCAPS2));
  236.     ddscaps.dwCaps=DDSCAPS_BACKBUFFER | DDSCAPS_3DDEVICE;
  237.     lpddsPrime->GetAttachedSurface(&ddscaps,&lpddsBack);
  238.  
  239.     //get the idirect3d pointer
  240.     lpdd->QueryInterface(IID_IDirect3D7,(void**)&lpd3d);//ICKY COM STUFF!
  241.  
  242.     //create the idirect3ddevice(hack method)
  243.     if(FAILED(lpd3d->CreateDevice(IID_IDirect3DTnLHalDevice,lpddsBack,&lpd3ddev)))//try tnl
  244.         if(FAILED(lpd3d->CreateDevice(IID_IDirect3DHALDevice,lpddsBack,&lpd3ddev)))//no tnl, try hal
  245.             if(FAILED(lpd3d->CreateDevice(IID_IDirect3DMMXDevice,lpddsBack,&lpd3ddev)))//no hal, try mmp
  246.                 if(FAILED(lpd3d->CreateDevice(IID_IDirect3DRGBDevice,lpddsBack,&lpd3ddev)))//no mmx, resort to rgb
  247.                     return(false);//problem, return false
  248.  
  249.  
  250.     //set up viewport
  251.     D3DVIEWPORT7 vp;
  252.     vp.dwX=0;
  253.     vp.dwY=0;
  254.     vp.dwWidth=SCREENWIDTH;
  255.     vp.dwHeight=SCREENHEIGHT;
  256.     vp.dvMinZ=0.0;
  257.     vp.dvMaxZ=1.0;
  258.  
  259.     //initialize the vertices(partially, anyway)
  260.     vert[0].color=D3DRGB(0.0,1.0,0.0);//set the color for this vertex
  261.     vert[0].specular=0;//zero for specular
  262.     vert[0].rhw=1.0;//rhw is 1.0
  263.     vert[0].tu=0.0;//0.0 for both texture coordinates
  264.     vert[0].tv=0.0;
  265.     vert[0].sz=0.5;//static z value
  266.  
  267.     vert[1].color=D3DRGB(0.0,0.0,1.0);//set the color for this vertex
  268.     vert[1].specular=0;//zero for specular
  269.     vert[1].rhw=1.0;//rhw is 1.0
  270.     vert[1].tu=0.0;//0.0 for both texture coordinates
  271.     vert[1].tv=0.0;
  272.     vert[1].sz=0.5;//static z value
  273.  
  274.     vert[2].color=D3DRGB(1.0,0.0,0.0);//set the color for this vertex
  275.     vert[2].specular=0;//zero for specular
  276.     vert[2].rhw=1.0;//rhw is 1.0
  277.     vert[2].tu=0.0;//0.0 for both texture coordinates
  278.     vert[2].tv=0.0;
  279.     vert[2].sz=0.5;//static z value
  280.  
  281.     //set viewport for device
  282.     lpd3ddev->SetViewport(&vp);
  283.  
  284.     return(true);//return success
  285. }
  286.  
  287. //////////////////////////////////////////////////////////////////////////////
  288. //CLEANUP
  289. //////////////////////////////////////////////////////////////////////////////
  290. void Prog_Done()
  291. {    
  292.     //release IDirect3DDevice
  293.     if(lpd3ddev)
  294.     {
  295.         lpd3ddev->Release();
  296.         lpd3ddev=NULL;
  297.     }
  298.  
  299.     //release IDirect3D 
  300.     if(lpd3d)
  301.     {
  302.         lpd3d->Release();
  303.         lpd3d=NULL;
  304.     }
  305.  
  306.     //clean up primary surface(this will clean up the back buffer, also)
  307.     LPDDS_Release(&lpddsPrime);
  308.  
  309.     //clean up the dd pointer
  310.     LPDD_Release(&lpdd);
  311. }
  312.  
  313. //////////////////////////////////////////////////////////////////////////////
  314. //MAIN GAME LOOP
  315. //////////////////////////////////////////////////////////////////////////////
  316. void Prog_Loop()
  317. {
  318.     //set up the vertex positions
  319.     vert[0].sx=cos(angle)*240.0+320.0;
  320.     vert[0].sy=sin(angle)*240.0+240.0;
  321.  
  322.     vert[1].sx=cos(angle+2*PI/3)*240.0+320.0;
  323.     vert[1].sy=sin(angle+2*PI/3)*240.0+240.0;
  324.  
  325.     vert[2].sx=cos(angle-2*PI/3)*240.0+320.0;
  326.     vert[2].sy=sin(angle-2*PI/3)*240.0+240.0;
  327.  
  328.     //add to the angle for next time
  329.     angle+=PI/180;
  330.  
  331.     //clear the viewport to black
  332.     lpd3ddev->Clear(0,NULL,D3DCLEAR_TARGET,0,0,0);
  333.  
  334.     //start the scene
  335.     lpd3ddev->BeginScene();
  336.  
  337.     //draw the triangle
  338.     lpd3ddev->DrawPrimitive(D3DPT_TRIANGLELIST,D3DFVF_TLVERTEX,vert,3,0);
  339.  
  340.     //end the scene
  341.     lpd3ddev->EndScene();
  342.  
  343.     //flip 
  344.     lpddsPrime->Flip(NULL,DDFLIP_WAIT);
  345. }
  346.  
  347.